﻿using Sharp.DataTransferObject;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
using Sharp.Infrastructure;

// ReSharper disable Mvc.ViewNotResolved

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [Authorize]
    [AutomaticTemplate]
    public class OrgLevelController : SharpController
    {
        public ActionResult Index()
        {
            return View(new OrgLevelIndexViewModel());
        }

        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(DataTableParameter parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var pageInfo = proxy.Channel.GetOrgLevelPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }

        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrgLevelDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetOrgLevelInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            return View(dto.To<OrgLevelModel>());
        }

        public ActionResult Create()
        {
            return View(new OrgLevelModel());
        }

        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrgLevelDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetOrgLevelInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }
            return View(dto.To<OrgLevelModel>());
        }


        public ActionResult Save(OrgLevelModel role)
        {
            Result res;
            var roleDto = role.To<OrgLevelDTO>();
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                res = proxy.Channel.SaveOrgLevel(roleDto);
            }

            return this.JsonNet(res);
        }


        public ActionResult Delete(Guid[] ids)
        {
            Result res;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                res = proxy.Channel.DeleteOrgLevel(ids);
            }

            return this.JsonNet(res);
        }
    }
}